/*
 * Copyright 2013 Mounir IDRASSI  (mounir.idrassi@idrix.fr, for IDRIX)
 *
 * This file is part of SimCardManager.
 *
 * SimCardManager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * SimCardManager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SimCardManager.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// PinDlg.h : interface of the CPinDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once


class CPinDlg : public CDialogImpl<CPinDlg>
{
public:
   enum { IDD = IDD_PIN_DIALOG };
   TCHAR m_szPIN[16];
   CEdit m_pin;
   int m_tries;

   BEGIN_MSG_MAP(CPinDlg)
      MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
      COMMAND_ID_HANDLER(IDOK, OnOK)
      COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
   END_MSG_MAP()


   // Handler prototypes (uncomment arguments if needed):
   //	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   //	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
   //	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

   LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
   {
      // center the dialog on the screen
      CenterWindow(GetParent());

      // set icons
      HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
         IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
      SetIcon(hIcon, TRUE);
      HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
         IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
      SetIcon(hIconSmall, FALSE);

      m_pin.Attach(GetDlgItem(IDC_PIN));
      m_pin.SetLimitText(8);

      TCHAR szLabel[512];
      _stprintf(szLabel, _T("PIN1 Tries left = %d"), m_tries);
      SetDlgItemText(IDC_TRIES, szLabel);

      SecureZeroMemory(m_szPIN, sizeof(m_szPIN));


      return TRUE;
   }

   static bool IsValid(LPCTSTR szPIN)
   {
      while (*szPIN)
      {
         if (!_istdigit(*szPIN))
            return false;
         szPIN++;
      }
      return true;
   }

   LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
   {
      GetDlgItemText(IDC_PIN, m_szPIN, sizeof(m_szPIN)/sizeof(TCHAR));
      if (_tcslen(m_szPIN) == 0)
      {
         MessageBox(_T("No PIN entered!"), _T("Empty PIN"), MB_ICONWARNING);
      }
      else if (_tcslen(m_szPIN) < 4)
      {
         MessageBox(_T("PIN too short!\nMust contain at least 4 digits."), _T("PIN short"), MB_ICONWARNING);
      }
      else if (!IsValid(m_szPIN))
      {
         MessageBox(_T("The PIN must contain only digits."), _T("Invalid PIN"), MB_ICONWARNING);
      }
      else
         EndDialog(wID);
      return 0;
   }

   LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
   {
      EndDialog(wID);
      return 0;
   }
};
